
package galcon.visuals.components;

import galcon.utils.Constants;
import galcon.utils.Drawable;
import galcon.utils.Point;
import java.awt.Color;
import java.awt.Graphics;

public class Ship implements Drawable {
	protected Point	cur;
	protected Planet	destination;
	protected int		ownerID;
	protected Color	color;
	protected int		actualShips;
	protected boolean	done;
	
	public Ship (Planet source, Planet destination, int ownerID, Color color, int actualShips) {
		this.destination = destination;
		this.ownerID = ownerID;
		this.color = color;
		this.actualShips = actualShips;
		done = false;
		cur = new Point (source.getCircle ().getPoint ().getX (), source.getCircle ().getPoint ().getY ());
	}
	
	public Planet getDestPlanet () {
		return destination;
	}
	
	public int getOwnerID () {
		return ownerID;
	}
	
	public Color getColor () {
		return color;
	}
	
	public void setDestPlanet (Planet destination) {
		this.destination = destination;
	}
	
	public void setOwnerID (int ownerID) {
		this.ownerID = ownerID;
	}
	
	public void setColor (Color color) {
		this.color = color;
	}
	
	public void setDone () {
		done = true;
	}
	
	public boolean isDone () {
		return done;
	}
	
	public boolean step () {
		if (done) return true;
		if (destination.getCircle ().getPoint ().distance (cur) <= destination.getCircle ().getRadius ()) {
			if (destination.getOwnerID () == this.ownerID) destination.addShips (actualShips);
			else if (destination.getShips () < actualShips) {
				destination.setOwnerID (this.ownerID);
				destination.addShips (actualShips - destination.getShips ());
				destination.setColor (color);
			} else destination.subtractShips (actualShips);
			return true;
		} else {
			double speed = Constants.PIXEL_MOVEMENT_PER_FRAME;
			double totalDist = destination.getCircle ().getPoint ().distance (cur);
			double frac = speed / totalDist;
			int movex = (int) ((destination.getCircle ().getPoint ().getX () - cur.getX ()) * frac);
			int movey = (int) ((destination.getCircle ().getPoint ().getY () - cur.getY ()) * frac);
			int nextx = cur.getX () + movex, nexty = cur.getY () + movey;
			cur = new Point (nextx, nexty);
			return false;
		}
	}
	
	public void draw (Graphics g) {
		g.setColor (color);
		int size = this.actualShips / 3 + 10;
		g.fillOval (this.cur.getX (), this.cur.getY (), size, size);
		g.setColor (Color.BLACK);
		g.drawOval (this.cur.getX (), this.cur.getY (), size, size);
	}
}
